<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\ocean;

use app\model\OeAssetsModel;
use app\model\OeOptimizedGoalNewModel;
use app\service\BaseService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class OptimizedGoalService extends BaseService
{
    /**
     * 获取资产信息.
     * @return bool|mixed|string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getAssets(string $advertiserId = '')
    {
        // 获取资产信息
        return OeAssetsModel::where('advertiser_id', $advertiserId)->select();
    }

    /**
     * @return bool|mixed|string
     */
    public function getAppData(string $token = '', string $advertiserId = '', array $params = [])
    {
        $headers = [
            'Access-Token' => $token,
            'Content-Type' => 'application/json',
        ];
        $params['advertiser_id'] = $advertiserId;
        return AdvertiserService::getOptimizedGoal($headers, $params);
    }

    /**
     * 获取信息.
     * @param string $advertiserId 账户id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAppInfo(string $advertiserId = '', array $info = []): array
    {
        $where = [
            ['advertiser_id', '=', $advertiserId],
            ['asset_id', '=', $info['asset_id']],
            ['landing_type', '=', $info['landing_type']],
            ['asset_type', '=', $info['asset_type']],
            ['external_action', '=', $info['external_action']],
            ['app_type', '=', $info['app_type']],
        ];

        $info = (new OeOptimizedGoalNewModel())->where($where)->field(true)->find();
        if (! $info) {
            return [];
        }

        return $info->toArray();
    }

    /**
     * 修改信息.
     * @param int $id 主键ID
     * @param array $info 信息数据
     */
    public function updateAdvertiserData(int $id = 0, array $info = []): bool
    {
        try {
            $data = [
                'external_action' => $info['external_action'],
                'optimization_name' => $info['optimization_name'],
                'history_back' => $info['history_back'],
                'twenty_four_hour_back' => $info['twenty_four_hour_back'],
                'deep_goals' => $info['deep_goals'] ? json_encode($info['deep_goals'], JSON_UNESCAPED_UNICODE) : [],
                'update_time' => time(),
            ];
            //            Db::startTrans();
            $one = (new OeOptimizedGoalNewModel())->where('id', $id)->update($data);
            if ($one) {
                //                Db::commit();
                return true;
            }
            //            Db::rollback();
            return false;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * 插入广告报表信息.
     * @param string $advertiserId 账户ID
     * @param array $info 信息数据
     */
    public function insertAdvertiserData(string $advertiserId = '', array $info = []): bool
    {
        try {
            //            Db::startTrans();
            $data = [
                'advertiser_id' => $advertiserId,
                'account_id' => $info['account_id'],
                'landing_type' => $info['landing_type'],
                'ad_type' => $info['ad_type'],
                'asset_type' => $info['asset_type'],
                'app_type' => $info['app_type'],
                'app_promotion_type' => $info['app_promotion_type'],
                'asset_id' => $info['asset_id'],
                'external_action' => $info['external_action'],
                'optimization_name' => $info['optimization_name'],
                'history_back' => $info['history_back'],
                'twenty_four_hour_back' => $info['twenty_four_hour_back'],
                'deep_goals' => $info['deep_goals'] ? json_encode($info['deep_goals'], JSON_UNESCAPED_UNICODE) : [],
                'create_time' => time(),
            ];
            $insertId = (new OeOptimizedGoalNewModel())->insertGetId($data);
            if ($insertId <= 0) {
                //                Db::rollback();
                return false;
            }
            //            Db::commit();
            return true;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }
}
